package com.ljy.my_study.spring.tiny.study7.context;

import java.util.Map;
import java.util.Map.Entry;

import com.ljy.my_study.spring.tiny.study7.beans.BeanDefinition;
import com.ljy.my_study.spring.tiny.study7.beans.factory.AbstractBeanFactory;
import com.ljy.my_study.spring.tiny.study7.beans.factory.AutowireCapableBeanFactory;
import com.ljy.my_study.spring.tiny.study7.beans.io.ResourceLoader;
import com.ljy.my_study.spring.tiny.study7.beans.xml.XmlBeanDefinitionReader;

public class ClassPathXmlApplicationContext extends AbstractApplicationContext{

	private String configLocation;
	
	public ClassPathXmlApplicationContext(String location) throws Exception {
		this(location,new AutowireCapableBeanFactory());
	}
	
	public ClassPathXmlApplicationContext(String location,AbstractBeanFactory beanFactory) throws Exception {
		super(beanFactory);
		this.configLocation=location;
		refresh();
	}

	private void refresh() throws Exception {
		XmlBeanDefinitionReader reader=new XmlBeanDefinitionReader(new ResourceLoader());
		reader.loadBeanDefinitions(configLocation);
		Map<String,BeanDefinition> beanDefinitionMap=reader.getRegistry();
		for(Entry<String,BeanDefinition> entry:beanDefinitionMap.entrySet()) {
			beanFactory.registerBeanDefinition(entry.getKey(), entry.getValue());
		}
	}
	
}
